//
//  Extra5ViewController.m
//  ToucheTouche
//
//  Created by polytech polytech on 13/01/12.
//  Copyright (c) 2012 polytech. All rights reserved.
//

#import "Extra5ViewController.h"
#import "ModeGameViewController.h"
#import "GameAventureViewController.h"
#import "Level.h"

@implementation Extra5ViewController
@synthesize labelScore;
@synthesize labelTemps;
@synthesize button1;
@synthesize button2;
@synthesize button3;
@synthesize button4;
@synthesize button5;
@synthesize imageView1;
@synthesize imageView2;
@synthesize imageView3;
@synthesize imageView4;
@synthesize imageView5;
@synthesize score,temps,allButtons,allImagesView,rand;
@synthesize gameAventureViewController;
@synthesize delegate;
@synthesize alertLevel;

/* action lorsqu'on touche un bouton */
-(IBAction)actionImage: (id)sender{
    for(NSInteger index=0;index < [allButtons count]; index++) {
        if([allButtons objectAtIndex:index]==sender){
            UIImageView *img = [allImagesView objectAtIndex:index];
            NSString *fileName = [NSString stringWithFormat: @"imageTest.png"];
            if(index==rand){                img.image =[UIImage imageNamed:fileName];
                score+=50;
                [[Level sharedLevel] initExtraScore:score];
                labelScore.text = [NSString stringWithFormat:@"%.0f", [[Level sharedLevel] floatExtraScore]];
            }
        }
    }
}

/* methode timer */
- (void)timer:(float) tmp{
    temps = tmp;
    chrono = [[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];
    // Affiche le compteur
    labelTemps.text = [NSString stringWithFormat:@"%d", temps];
}

/* methode affichant le chrono */
- (void) updateTimer: (NSTimer *) aTimer{
    temps-=0.5;
    // Affiche l'état du compte à rebour    
    labelTemps.text = [NSString stringWithFormat:@"%d", temps];
    
    [self changeImage];
    
    // Arrète le NSTimer lorsque le compte à rebour est à zéro
    if ( temps <= 0 ) {
        [self stopTimer];
    }
}

/* methode arret timer */
- (void)stopTimer {
    [chrono invalidate];
    chrono = nil;
    
    // Affiche l'alerte avec le score
    alertLevel = [[UIAlertView alloc] initWithTitle:@"Score de l'extra" message:[NSString stringWithFormat:@"%.0f \n\nPassage au niveau 6", [[Level sharedLevel] floatExtraScore]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertLevel show];
    [alertLevel release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  {
    if (alertView == alertLevel) {
        if (buttonIndex == 0) {
            
            // ajout du score de l'extra au score global
            [self.gameAventureViewController addScoreNextExtra];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"AddScoreNextExtraNotification" object:nil];
            
            // mise a jour du nouveau niveau [bonnes valeurs]
            // init : score, score to add, score for extra, life, life to remove, lifeprogress, lifeprogress to remove, time level, time level to remove, current level, time image
            //[[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :30 :1500 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :31 :1 :6 :0.6];
            
            // POUR LES TESTS
            [[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :30 :20 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :10 :1 :6 :0.5];
            
            
            
            NSLog(@"Score actuel : %.0f", [[Level sharedLevel] floatScore]);
            NSLog(@"Score a ajouter : %.0f", [[Level sharedLevel] floatScoreToAdd]);
            NSLog(@"vie actuelle : %.0f", [[Level sharedLevel] floatLife]);
            NSLog(@"vie a enlever : %.0f", [[Level sharedLevel] floatLifeToRemove]);
            NSLog(@"lifeprogress : %.0f", [[Level sharedLevel] floatLifeProgress]);
            NSLog(@"lifeprogress to remove : %.2f", [[Level sharedLevel] floatLifeProgressToRemove]);
            NSLog(@"temps du niveau : %d", [[Level sharedLevel] intTimeLevel]);
            NSLog(@"numero niveau : %d", [[Level sharedLevel] intCurrentLevel]);
            
            
            [self.gameAventureViewController initLabel]; // init des labels
            [[NSNotificationCenter defaultCenter] postNotificationName:@"InitLabelNotification" object:nil];
            [self.gameAventureViewController timerImage]; // deplacement image
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerNotification" object:nil];
            [self.gameAventureViewController timerLevel:[[Level sharedLevel] intTimeLevel]]; // temps du niveau
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerLevelNotification" object:nil];
            
            
            
            
            // Redirection vers le jeu
            [self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self dismissModalViewControllerAnimated:YES];
            
        }
    }
}


/* methode permettant de changer l'image */
-(void) changeImage{
    NSString *fileName = [NSString stringWithFormat: @"imageTest.png"];
    UIImageView *img = [allImagesView objectAtIndex:rand];
    img.image = [UIImage imageNamed:fileName];
    
    rand = (arc4random() % 4);

    fileName = [NSString stringWithFormat: @"imageTest2.png"];
    img = [allImagesView objectAtIndex:rand];
    img.image = [UIImage imageNamed:fileName];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    temps=0;
    score=0;
    [[Level sharedLevel] initExtraScore:score];
    labelScore.text = [NSString stringWithFormat:@"%.0f", score];
    rand=0;
    allImagesView = [[NSArray alloc] initWithObjects:imageView1, imageView2,imageView3,imageView4,imageView5, nil];
    
    allButtons = [[NSArray alloc] initWithObjects:button1,button2,button3,button4,button5, nil];
    
    
    NSString *fileName = [NSString stringWithFormat: @"imageTest.png"];
    for (UIImageView *imageview in allImagesView) {
        imageview.image = [UIImage imageNamed:fileName];
    }
    [self timer:5];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setButton1:nil];
    [self setButton2:nil];
    [self setButton3:nil];
    [self setButton4:nil];
    [self setButton5:nil];
    [self setImageView1:nil];
    [self setImageView2:nil];
    [self setImageView3:nil];
    [self setImageView4:nil];
    [self setImageView5:nil];
    [self setLabelScore:nil];
    [self setLabelTemps:nil];
    for (UIButton *buttons in allButtons) {
        buttons = nil;
    }
    
    for (UIImageView *imageview in allImagesView) {
        imageview = nil;
    }
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)dealloc {
    [button1 release];
    [button2 release];
    [button3 release];
    [button4 release];
    [button5 release];
    [imageView1 release];
    [imageView2 release];
    [imageView3 release];
    [imageView4 release];
    [imageView5 release];
    [labelScore release];
    [labelTemps release];
    for (UIButton *buttons in allButtons) {
        [buttons release];
    }
    
    for (UIImageView *imageview in allImagesView) {
        [imageview release];
    }
    [allButtons release];
    [allImagesView release];
    [super dealloc];
}
@end
